import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model


# predict the house price with linear regression
# read train-set file


def get_data(file_name):
    data = pd.read_csv(file_name)
    X_parameters = []
    Y_parameters = []
    for single_square_feet, single_price_value in zip(data['square_feet'], data['price']):
        X_parameters.append([float(single_square_feet)])
        Y_parameters.append([float(single_price_value)])
    return X_parameters,Y_parameters


def linear_model_main(X_parameters, Y_parameters, predict_value):
    # Create linear model
    regr = linear_model.LinearRegression()
    regr.fit(X_parameters, Y_parameters)
    predict_outcome = regr.predict(predict_value)
    # intercept
    # coefficient
    predictions = {'intercept': regr.intercept_, 'coefficient': regr.coef_, 'predict_outcome': predict_outcome}
    return predictions


def show_linear_line(X_parameters, Y_parameters):
    regr = linear_model.LinearRegression()
    regr.fit(X_parameters, Y_parameters)
    plt.scatter(X_parameters, Y_parameters, color='blue')
    plt.plot(X_parameters, regr.predict(X_parameters), color='red')
    plt.xticks(())
    plt.yticks(())
    plt.show()


# get training set from csv file
X, Y = get_data('input_data.csv')
# predict 700 feet house
predict_value = 700
# new version sklearn need array(vector)
predict_value = np.array(predict_value).reshape(1, -1)
result = linear_model_main(X, Y, predict_value)
# print(result)
print("Intercept value ", result['intercept'])
print("coefficient", result['coefficient'][0])
print("Predicted value: ", result['predict_outcome'][0])
show_linear_line(X, Y)